package com.framework.controller.newcontroller;

import com.framework.entity.tran.TranOrder;
import com.framework.exception.util.RespResult;
import com.framework.service.TranOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 订单控制器
 *
 * @author ck
 * @date 2018/9/18 18:39
 */
@RestController
@RequestMapping(value = "new/orders")
@Api(value="订单模块",tags={"订单模块"})
public class NewOrdersController {

	@Autowired
	private TranOrderService tranOrderService;

	/**
	 *添加订单信息
	 * @param token 当前登录人Token
	 * @param goodsId 商品id
	 * @param goodsNumber 数量
	 * @param goodsPrice 总价格
	 * @return
	 */
	@PostMapping("user/addOrders")
	@ApiOperation("添加订单信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="goodsId",value="商品id",dataType="long", paramType = "query" ,required = true),
			@ApiImplicitParam(name="goodsNumber",value="购买数量",dataType="int", paramType = "query",required = true),
			@ApiImplicitParam(name="goodsPrice",value="商品总价格",dataType="bigDecimal", paramType = "query",required = true),
			@ApiImplicitParam(name="token",value="登录产生得token",dataType="string", paramType = "query",required = true)
	})
	public RespResult addOrders(String token,Long goodsId,Integer goodsNumber,BigDecimal goodsPrice) {
		String orderNo = tranOrderService.addOrder(token,goodsId,goodsNumber,goodsPrice);
		return RespResult.ok().put("orderNo",orderNo);
	}

	/**
	 * 订单列表查询
	 * @param token 当前人token
	 * @param orderState 订单状态
	 * @return
	 */
	@GetMapping("user/queryOrdersList")
	@ApiOperation("订单列表查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderState",value="订单状态 -1 全部 0 待付款 1待发货2，待签收3，已完成",dataType="string", paramType = "query" ,required = true),
			@ApiImplicitParam(name="token",value="登录产生得token",dataType="string", paramType = "query",required = true)
	})
	public RespResult queryOrdersList(String token, Integer orderState ) {
		List<TranOrder> list = tranOrderService.queryOrdersList(token,orderState);
		return RespResult.ok().put("orderList",list);
	}

	/**
	 * 根据订单号查询物流信息
	 * @param token token
	 * @param orderNo 订单号
	 * @return
	 */
	@GetMapping("user/queryLogistics")
	@ApiOperation("根据订单号查询物流信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true),
			@ApiImplicitParam(name="token",value="登录产生得token",dataType="string", paramType = "query",required = true)
	})
	public RespResult queryLogistics(String token, String orderNo) {
		Map<String,Object> map = tranOrderService.queryLogistics(token,orderNo);
		return RespResult.ok().put("data",map);
	}

	/**
	 * 取消订单
	 * @param token 当钱登陆人token
	 * @param orderNo 订单号
	 * @return
	 */
	@PostMapping("user/cancelOrders")
	@ApiOperation("取消订单")
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true ),
			@ApiImplicitParam(name="token",value="登录产生得token",dataType="string", paramType = "query",required = true)

	})
	public RespResult cancelOrders(String token,String orderNo) {
		tranOrderService.cancelOrders(token,orderNo);
		return RespResult.ok();
	}

	/**
	 * 订单签收
	 * @param token
	 * @param orderNo
	 * @return
	 */
	@PostMapping("user/signOrders")
	@ApiOperation("订单签收")
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true ),
			@ApiImplicitParam(name="token",value="登录产生得token",dataType="string", paramType = "query",required = true)
	})

	public RespResult signOrders(String token,String orderNo) {
		tranOrderService.signOrders(token,orderNo);
		return RespResult.ok();
	}

	/**
	 * 查询订单详情
	 * @param orderNo
	 * @return
	 */
	@GetMapping("user/queryOrderInfo")
	@ApiOperation("查询订单详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query" ,required = true),

})
	public RespResult queryOrderInfo(String orderNo) {
		Map<String,Object> map = tranOrderService.queryOrderInfo(orderNo);
		return RespResult.ok().put("data",map);
	}


	/**
	 * 删除订单
	 * @param orderNo
	 * @return
	 */
	@PostMapping("user/deleteOrder")
	@ApiOperation("删除订单")
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query" ,required = true),
			@ApiImplicitParam(name="token",value="登录产生得token",dataType="string", paramType = "query",required = true)
	})
	public RespResult deleteOrder(String token,String orderNo) {
		tranOrderService.deleteOrder(token,orderNo);
		return RespResult.ok();
	}


	/**
	 * 支付操作
	 * @param token
	 * @param orderNo
	 * @param payType
	 * @param password
	 * @return
	 */
	@PostMapping("pay")
	@ApiOperation("支付操作")
	@ApiImplicitParams({
			@ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true ),
			@ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true ),
			@ApiImplicitParam(name="payType",value="支付方式 1：支付宝",dataType="int", paramType = "query",required = true ),
			@ApiImplicitParam(name="password",value="交易密码",dataType="String", paramType = "query")
	})
	public RespResult pay(String token,String orderNo,Integer payType,String password){
		String res = tranOrderService.pay(token,orderNo,payType,password);
		return RespResult.ok().put("res",res);
	}

}
